package org.htzb.news.dao.impl;

import org.htzb.news.dao.C3P0Util;
import org.htzb.news.dao.TopicDao;
import org.htzb.news.entity.Topic;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TopicDaoImpl extends C3P0Util implements TopicDao {
    @Override
    public List<Topic> getAllTopic() {
        List<Topic> topiclist = new ArrayList<Topic>();
        String sql = "select * from topic";
        try {
            connection = getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                int tid = resultSet.getInt("TID");
                String title = resultSet.getString("TNAME");

                Topic topic = new Topic();
                topic.setTid(resultSet.getInt("tid"));
                topic.setTname(resultSet.getString("tname"));
                topiclist.add(topic);

                System.out.println("该新闻的类别编号为：" + tid);
                System.out.println("该新闻的分类板块为：" + title);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            realease(connection, statement, preparedStatement, resultSet);
        }
        return topiclist;
    }

    private void realease(Connection connection, Statement statement, PreparedStatement preparedStatement, ResultSet resultSet) {
    }
    public static void main(String[] args) {
        TopicDaoImpl TopicDao = new TopicDaoImpl();
        List<Topic> topiclist = TopicDao.getAllTopic();
        System.out.println("总共有" + topiclist.size()+"条新闻类别");
    }

}

